var action =
(function(){

	var gmail_container = document.getElementById("canvas_frame").contentDocument;
	var _document = gmail_container.body;
	var search_input_el = _document.querySelector("form[name=gbqf] input[name=q]");
	var search_button_el = _document.querySelector("form[name=gbqf] button");

	console.log(search_input_el);
	console.log(search_button_el);
	
	var _fireEvent = function(e,t)
	{
		if( t && e )
		{
			var cancelled = t.dispatchEvent(e);
			console.log("Event cancelled? " + cancelled);
		}
	}
	
	var _formatYYYYMMDD = function(d)
	{
		if(d)
		{
			return d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
		}
	}

	var GmailDateAction = function(){};

	GmailDateAction.prototype.filterMails = function(day)
	{
		try{

			console.log('filterTodaysMail');

			var evt = document.createEvent("MouseEvents");
			evt.initMouseEvent("click",true,true,_document,0,0,0,0,0,false,false,false,0,null);

			var NOW = new Date();

			var TODAY = new Date(new Date().setDate(NOW.getDate() + day ));
			
			var TODAY_P_1 = new Date(new Date().setDate(NOW.getDate() + (day+1) ));

			var query = "after:" + (_formatYYYYMMDD(TODAY)) + " before:" + (_formatYYYYMMDD(TODAY_P_1)) + " in:anywhere -in:trash";
		
			if( typeof(search_input_el) != 'undefined' )
			{
				search_input_el.value = query;
			}

			_fireEvent(evt, search_button_el);
		}
		catch(e) {}
	};

	GmailDateAction.prototype.filterTodayMails = function()
	{
		this.filterMails(0);
	};

	GmailDateAction.prototype.filterYesterdayMails = function()
	{
		this.filterMails(-1);
	};


	

	return new GmailDateAction();
})();
